﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using App_Code.PublicHelper;
using System.IO;

public partial class admin_dataproduct_editor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //int id = string.IsNullOrEmpty(Request.QueryString["s"]) ? 1 : int.Parse(Request.QueryString["s"]);

            if (!string.IsNullOrEmpty(Request.QueryString["s"]))
            {
                int id = int.Parse(Request.QueryString["s"]);
                string queStr = "select * from dataproduct where id=" + id;
                DataSet ds = DatabaseHelper.ExecuteDataset(queStr);
                if (ds == null || ds.Tables.Count == 0)
                {
                    return;
                }
                DataTable dt = ds.Tables[0];
                if (dt.Rows.Count == 0)
                {
                    return;
                }
                DataRow row = dt.Rows[0];
                TxtId.Text = row.ItemArray[0].ToString();
                TxtTitle.Text = row.ItemArray[1].ToString();
                TxtField.Text = row.ItemArray[5].ToString();
                TxtDes.Text = row.ItemArray[2].ToString();
                mImg.Src = "../../file/dataproduct/thumbnail/" + row.ItemArray[3].ToString();
                htmlName.Text = row.ItemArray[4].ToString();
                string htmlfilename = Server.MapPath("~/file/dataproduct/html") + "\\" + row.ItemArray[4].ToString();
                FileStream fs = new FileStream(htmlfilename, FileMode.Open, FileAccess.ReadWrite);
                StreamReader sr = new StreamReader(fs);
                string htmlstr = sr.ReadToEnd();
                sr.Close();
                content1.Value = htmlstr;
            }

        }
    }

    protected void Button1_Click(object sender, System.EventArgs e)
    {
        string sid = TxtId.Text;

        if (string.IsNullOrEmpty(sid))
        {

            InsertData();
            return;
        }

        int id = int.Parse(sid);
        string htmlfilename = htmlName.Text;
        string strTitle = TxtTitle.Text;
        string strField = TxtField.Text;
        string strDes = TxtDes.Text;
        string imgfile = srcHidden.Text.ToString();
        string sql;
        string htmlstr = Request.Form[content1.UniqueID];

        try
        {
            WriteHTML(htmlfilename, htmlstr);
        }
        catch (Exception exception)
        {

            Page.ClientScript.RegisterStartupScript(this.GetType(), "error", "<script language=javascript>alert('html存储失败');</script>");
            return;
        }

        if (!string.IsNullOrEmpty(imgfile))
        {
            string[] s = imgfile.Split('/');
            string filename = s[s.Length - 1];
            try
            {
                CopyImage(filename);
                sql = string.Format("update dataproduct set NAME = '{1}', SUMMARY = '{2}',IMAGE = '{3}',HTML = '{4}',FIELD='{5}' where id={0}", id, strTitle, strDes, filename, htmlfilename, strField);
            }
            catch (Exception)
            {

                Page.ClientScript.RegisterStartupScript(this.GetType(), "error", "<script language=javascript>alert('图片转存失败');</script>");
                return;
            }


        }
        else
        {
            sql = string.Format("update dataproduct set NAME = '{1}', SUMMARY = '{2}', HTML = '{3}',FIELD='{4}'where id={0}", id, strTitle, strDes, htmlfilename, strField);
        }
        if (!string.IsNullOrEmpty(sql))
        {
            int k = DatabaseHelper.ExecuteNonQuery(sql);
            if (k != -1)
            {
                Response.Redirect("admin.aspx");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "error", "<script language=javascript>alert('编辑失败');</script>");
            }
        }

    }

    public void InsertData()
    {

        string strTitle = TxtTitle.Text;
        string strField = TxtField.Text;
        string strDes = TxtDes.Text;
        string imgfile = srcHidden.Text.ToString();
        string sql;
        string swss = content1.Value;
        string htmlstr = Request.Form[content1.UniqueID];
        string htmlfilename = System.Guid.NewGuid().ToString() + ".htm";
        try
        {
            //写html文件            
            WriteHTML(htmlfilename, htmlstr);
        }
        catch (Exception exception)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "error", "<script language=javascript>alert('html存储失败');</script>");
            return;
        }

        string[] s = imgfile.Split('/');
        string filename = s[s.Length - 1];

        try
        {
            CopyImage(filename);
            sql = string.Format("insert into dataproduct (ID,NAME,SUMMARY,IMAGE,HTML,FIELD) values ({0},'{1}','{2}','{3}','{4}','{5}')", "SEQ_DATAPRODUCT.nextval", strTitle, strDes, filename, htmlfilename, strField);
        }
        catch (Exception)
        {

            Page.ClientScript.RegisterStartupScript(this.GetType(), "error", "<script language=javascript>alert('图片转存失败');</script>");
            return;
        }

        if (!string.IsNullOrEmpty(sql))
        {
            int k = DatabaseHelper.ExecuteNonQuery(sql);
            if (k != -1)
            {
                Response.Redirect("admin.aspx");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "error", "<script language=javascript>alert('编辑失败');</script>");
            }
        }
    }

    public void WriteHTML(string filename, string str)
    {
        //写html文件            
        string htmlfilename1 = Server.MapPath("~/file/dataproduct/html") + "\\" + filename;
        FileStream fs = new FileStream(htmlfilename1, FileMode.Create, FileAccess.ReadWrite);
        StreamWriter sw = new StreamWriter(fs);
        fs.SetLength(0);
        sw.Write(str);
        sw.Close();
    }

    public void CopyImage(string filename)
    {
        string path = Server.MapPath("~/file/temp");
        string path1 = Server.MapPath("~/file/dataproduct/thumbnail");
        File.Copy(path + "\\" + filename, path1 + "\\" + filename);
        File.Delete(path + "\\" + filename);
    }
}
